package com.F01.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.F01.actionForm.F010103ActionForm;
import com.F01.actionForm.F010105ActionForm;
import com.dao.common.DbOperationExt;
import com.dbtools.Dispose;
import com.util.CommonTools;

public class F010105DAO extends DbOperationExt {
	/**
	 * 记录操作日志;
	 */
	Logger log = Logger.getLogger(F010103DAO.class);

	/**
	 * 构造方法获取数据源;
	 * 
	 */
	public F010105DAO() {
		super();
	}

	public F010105DAO(Connection conn) {
		super(conn);
	}

	/**
	 * @查询
	 */
	public List QUERYLIST(F010105ActionForm actionForm,
			HttpServletRequest request) {
		StringBuffer msql = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		/** 判断查询条件，拼装查询SQL */
		/** 订单号码 */
		if (actionForm.getCotract_number() != null
				&& !actionForm.getCotract_number().equals("")) {
			condition.append(" and b.cotract_number = '"
					+ actionForm.getCotract_number().trim() + "' ");
		}
		/** 工 作 号 */
		if (actionForm.getManufacturing_order() != null
				&& !actionForm.getManufacturing_order().equals("")) {
			condition.append(" and a.manufacturing_order = '"
					+ actionForm.getManufacturing_order().trim() + "' ");
		}
		/** 入库时间 */
		if ((actionForm.getSend_date_begin() != null && !"".equals(actionForm
				.getSend_date_begin()))
				|| (actionForm.getSend_date_end() != null && !""
						.equals(actionForm.getSend_date_end()))) {

			if (!"".equals(actionForm.getSend_date_begin())
					&& !"".equals(actionForm.getSend_date_end())) {
				condition.append(" and send_date BETWEEN '"
						+ actionForm.getSend_date_begin().trim() + "'");
				condition.append(" and '"
						+ actionForm.getSend_date_end().trim() + "' ");
			}
			if (!"".equals(actionForm.getSend_date_begin())
					&& "".equals(actionForm.getSend_date_end())) {
				condition.append(" and send_date >='"
						+ actionForm.getSend_date_begin().trim() + "'");
			}
			if ("".equals(actionForm.getSend_date_end())
					&& !"".equals(actionForm.getSend_date_begin())) {
				condition.append(" and send_date >='"
						+ actionForm.getSend_date_end() + "'");
			}
		}
		List list = new ArrayList();
		Map map = null;
		ResultSet rest = null;

		msql.append(" SELECT ");
		msql.append(" adyh.FUN_C_CUSTOMERS(b.customer_code) as customer_code, ");
		msql.append(" b.cotract_number as cotract_number, ");
		msql.append(" a.manufacturing_order as manufacturing_order, ");
		msql.append(" a.groups as groups, ");
		msql.append(" a.take_over as take_over,");
		msql.append(" a.contact_telephone as contact_telephone, ");
		msql.append(" a.send_locus as send_locus, ");
		msql.append(" a.send_amount as send_amount, ");
		msql.append(" a.send_date as send_date, a.remark as remark");
		msql.append(" FROM ");
		msql.append(" pd_consignment a ");
		msql.append(" left join c_orders b on a.manufacturing_order=b.manufacturing_order");
		msql.append(" where 1=1 " + condition.toString() + " ");
		msql.append(" order by  manufacturing_order ");
		try {
			log.info("成品库存查询 QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				map = new HashMap();
				map.put("cotract_number", CommonTools.null2String(rest
						.getString("cotract_number")));
				map.put("manufacturing_order", CommonTools.null2String(rest
						.getString("manufacturing_order")));
				map.put("customers_code", CommonTools.null2String(rest
						.getString("customer_code")));
				map.put("groups", CommonTools.null2String(rest
						.getString("groups")));
				map.put("take_over", CommonTools.null2String(rest.getString("take_over")));
				map.put("contact_telephone", CommonTools.null2String(rest
						.getString("contact_telephone")));
				map.put("send_locus",CommonTools.null2String(rest
								.getString("send_locus")));
				map.put("send_amount", CommonTools.null2String(rest
								.getString("send_amount")));
				map.put("send_date", CommonTools.getDateTime(rest
								.getDate("send_date")));
				map.put("remark", CommonTools.null2String(rest
								.getString("remark")));
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}

		return list;
	}
}
